package com.hjm.ui.order.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.order.entity.Order;

import java.util.List;

public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.OrderItemAdapterViewHolder> {
    List<? extends Order> orderList;
    OnItemClickListener onItemClickListener;

    public OrderItemAdapter(List<? extends Order> orderList) {
        this.orderList = orderList;
    }

    @NonNull
    @Override
    public OrderItemAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_item,parent, false);
        return new OrderItemAdapterViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderItemAdapterViewHolder holder, int position) {
        Order order = orderList.get(position);
        holder.orderItemName.setText(order.getGoodsName());
        holder.orderItemType.setText(order.getGoodsType());
        holder.orderItemNum.setText(order.getGoodsNum()+"");
        holder.orderItemTotalPrice.setText(order.getGoodsNum()* order.getPrice().floatValue()+"");
        holder.orderItemImage.setImageBitmap(order.getGoodsIcon());
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    class OrderItemAdapterViewHolder extends RecyclerView.ViewHolder {
        TextView orderItemName, orderItemType, orderItemNum, orderItemTotalPrice;
        ImageView orderItemImage;
        LinearLayout orderItemLayout;
        public OrderItemAdapterViewHolder(@NonNull View itemView) {
            super(itemView);

            orderItemName = itemView.findViewById(R.id.text_view_order_item_name);
            orderItemType = itemView.findViewById(R.id.text_view_order_item_type);
            orderItemNum = itemView.findViewById(R.id.text_view_order_item_num);
            orderItemTotalPrice = itemView.findViewById(R.id.text_view_order_item_price);
            orderItemImage = itemView.findViewById(R.id.image_view_order_item_img);
            orderItemLayout = itemView.findViewById(R.id.item_order_item);
            if (onItemClickListener != null) {
                orderItemLayout.setOnClickListener((v -> {
                    onItemClickListener.onItemClick(orderList.get(getLayoutPosition()));
                }));
            }
        }

    }

    public void setOrderList(List<? extends Order> orderList) {
        Log.d("订单适配器", "setOrderList: 设置订单列表 "+orderList.size());
        this.orderList = orderList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void onItemClick(Order order);
    }
}
